package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ScStudentContact;
import com.ruoyi.system.domain.ScStudentCourse;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.StudentApp;
import com.ruoyi.system.mapper.ScStudentContactMapper;
import com.ruoyi.system.mapper.ScStudentCourseMapper;
import com.ruoyi.system.mapper.StudentMapper;
import com.ruoyi.system.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 学生表Service业务层处理
 * 
 * @author ZHANGQIBO
 * @date 2023-12-14
 */
@Transactional(rollbackFor = {Exception.class})
@Service
public class StudentServiceImpl implements IStudentService
{
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ScStudentContactMapper scStudentContactMapper;


    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ScStudentCourseMapper scStudentCourseMapper;
    /**
     * 查询学生表
     * 
     * @param id 学生表主键
     * @return 学生表
     */
    @Override
    public Student selectStudentById(Long id)
    {
        Student student = studentMapper.selectStudentById(id);
        student.setContactList(scStudentContactMapper.selectScStudentContactBystudentId(id));
        return student;
    }

    @Override
    public Student selectStudentByIdApp(Long id) {
        Student student = studentMapper.selectStudentById(id);
        student.setContactList(scStudentContactMapper.selectScStudentContactBystudentId(id));
        ScStudentCourse scStudentCourse = new ScStudentCourse();
        scStudentCourse.setStudentId(id);
        student.setScStudentCourseList(scStudentCourseMapper.selectScStudentCourseList(scStudentCourse));
        return student;
    }

    @Override
    public List<StudentApp> selectStudentByApp(HttpServletRequest request) {
        String searchTitle = request.getParameter("searchTitle");
        String searchOrderBy = request.getParameter("searchOrderBy");
        String searchClaId = request.getParameter("searchClaId");

        if (searchClaId != "" ){
            String[] parts = searchClaId.split("-");
             searchClaId = parts[parts.length - 1]; // 获取最后一个部分
        }
        int num = Integer.parseInt(request.getParameter("num"));
        int page = Integer.parseInt(request.getParameter("page"));


        List<StudentApp> studentApps = studentMapper.selectByApp(searchTitle,searchOrderBy, searchClaId
                ,num,(page-1)*num);
        return studentApps;
    }

    /**
     * 查询学生表列表
     * 
     * @param student 学生表
     * @return 学生表
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        List<Student> students = studentMapper.selectStudentList(student);
        // 保存联系人信息
        if (null != students && students.size() > 0) {
            students.forEach(item -> {

                List<ScStudentContact> scStudentContacts = scStudentContactMapper.selectScStudentContactBystudentId(item.getId());
                item.setContactList(scStudentContacts);
            });


        }

        return students;
    }
    /**
     * 模糊查询学生表列表
     *
     * @param student 学生表
     * @return 学生表
     */
    @Override
    public List<Student> select(Student student) {
        return studentMapper.selects(student);
    }

    /**
     * 新增学生表
     * 
     * @param student 学生表
     * @return 结果
     */
    @Override
    public int insertStudent(Student student)
    {   int message= studentMapper.insertStudent(student);
        // 删除联系人信息
        scStudentContactMapper.deleteScStudentContactByStudentId(student.getId());

        List<ScStudentContact> contactList = student.getContactList();

        // 保存联系人信息
        if (null != contactList && contactList.size() > 0) {
            contactList.forEach(item -> {
                item.setStudentId(student.getId());
                item.setCreateUser(SecurityUtils.getUsername());
                scStudentContactMapper.insertScStudentContact(item);
            });


        }

        return message;
    }

    /**
     * 修改学生表
     * 
     * @param student 学生表
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        // 删除联系人信息
        scStudentContactMapper.deleteScStudentContactByStudentId(student.getId());
        List<ScStudentContact> contactList = student.getContactList();

        // 保存联系人信息
        if (null != contactList && contactList.size() > 0) {
            contactList.forEach(item -> {
                item.setStudentId(student.getId());
                item.setCreateUser(SecurityUtils.getUsername());
                scStudentContactMapper.insertScStudentContact(item);
            });}
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生表
     * 
     * @param ids 需要删除的学生表主键
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(Long[] ids)
    {
        for (Long id:
             ids) {
            scStudentContactMapper.deleteScStudentContactByStudentId(id);

        }

        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生表信息
     * 
     * @param id 学生表主键
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id)
    {
        scStudentContactMapper.deleteScStudentContactByStudentId(id);
        return studentMapper.deleteStudentById(id);
    }
}
